package com.jnr.huatu.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jnr.huatu.R;
import com.jnr.huatu.bean.ListViewItem;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * 多图显示adapter
 * @author：景辉
 * created at 2017/2/15 0015 下午 3:14
 */

public class ListViewAdapter extends BaseAdapter {
    private ArrayList<ListViewItem> mArrayList;
    private Context mContext;
    private ImageLoader mImageLoader;
    private DisplayImageOptions mDisplayImageOptions;
    private ImageLoadingListenerImpl mImageLoadingListenerImpl;

    public ListViewAdapter(ArrayList<ListViewItem> arrayList,Context context, ImageLoader imageLoader) {
        super();
        this.mArrayList = arrayList;
        this.mContext = context;
        this.mImageLoader = imageLoader;
        int defaultImageId = R.mipmap.ic_launcher;
        mDisplayImageOptions = new DisplayImageOptions.Builder()
//                .showStubImage(defaultImageId)
//                .showImageForEmptyUri(defaultImageId)
//                .showImageOnFail(defaultImageId)
//                .cacheInMemory(true)
//                .cacheOnDisc(true)
//                .resetViewBeforeLoading()
//                .build();
                .showImageOnLoading(R.drawable.logo)//开始下载显示图片
                .showImageOnFail(R.drawable.jh)//下载错误显示图片
                .cacheInMemory(true)//设置图片是否缓存到内存中
                .cacheOnDisk(true)//设置图片是否缓存到SD卡中
                .bitmapConfig(Bitmap.Config.RGB_565)//设置图片解码类型
                .build();
        mImageLoadingListenerImpl=new ImageLoadingListenerImpl();
    }

    public int getCount() {
        return mArrayList != null?mArrayList.size():0;

    }

    public Object getItem(int position) {
        return mArrayList !=null?mArrayList.get(position):0;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if (convertView==null) {
            holder=new ViewHolder();
            convertView= LayoutInflater.from(this.mContext).inflate(R.layout.listviewitem, null, false);
            holder.textView=(TextView) convertView.findViewById(R.id.textView);
            holder.imageView=(ImageView) convertView.findViewById(R.id.imageview);
            convertView.setTag(holder);
        } else {
            holder=(ViewHolder) convertView.getTag();
        }

        if (this.mArrayList!=null) {
            ListViewItem listViewItem=this.mArrayList.get(position);
            if (holder.textView!=null) {
                holder.textView.setText(listViewItem.getContent());
            }
            if (holder.imageView!=null) {
                try {
                    //加载图片
                    mImageLoader.displayImage(
                            listViewItem.getImageURL(),
                            holder.imageView,
                            mDisplayImageOptions,
                            mImageLoadingListenerImpl);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return convertView;
    }

    //监听图片异步加载
    public static class ImageLoadingListenerImpl extends SimpleImageLoadingListener {

        public static final List<String> displayedImages =
                Collections.synchronizedList(new LinkedList<String>());

        @Override
        public void onLoadingComplete(String imageUri, View view,Bitmap bitmap) {
            if (bitmap != null) {
                ImageView imageView = (ImageView) view;
                boolean isFirstDisplay = !displayedImages.contains(imageUri);
                if (isFirstDisplay) {
                    //图片的淡入效果
                    FadeInBitmapDisplayer.animate(imageView, 500);
                    displayedImages.add(imageUri);
                    System.out.println("===> loading "+imageUri);
                }
            }
        }
    }
    private class ViewHolder{
        ImageView imageView;
        TextView textView;
    }
}
